package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * category表现层
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 分类表的分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> listCategory(Integer page, Integer pageSize){
        //获取前端页面传过来的参数封装进page对象
        Page<Category> pageBean = new Page<>(page, pageSize);

        //创建条件构造器,根据sort进行排序
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        //执行查询语句
        categoryService.page(pageBean,queryWrapper);
        return R.success(pageBean);
    }

    /**
     * 分类表的添加方法
     * @param category
     * @return
     */
    @PostMapping
    public R addCategory(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加成功");
    }

    /**
     * 分类表的数据删除
     * @param id
     * @return
     * @throws BusinessException
     */
    @DeleteMapping
    public R deleteCategory(Long id) throws BusinessException {
        //创建条件构造器,根据列名category_id=id
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",id);

        //执行sql语句:select count(*) from dish where category_id = ?
        int count = dishService.count(queryWrapper);
        //如果查询出来的数据不止一条,说明菜品跟分类表这条数据有关联,则抛出自定义异常BusinessException
        if(count > 0){
            throw new BusinessException("当前分类下有菜品,不能删除");
        }

        //执行sql语句:select count(*) from setmeal where category_id = ?
        int count1 = setmealService.count(queryWrapper);
        //如果查询出来的数据不止一条,说明菜品跟套餐表这条数据有关联,则抛出自定义异常BusinessException
        if(count1 > 0){
            throw new BusinessException("当前分类下有套餐,不能删除");
        }
        categoryService.removeById(id);
        return R.success("成功");
    }

    /**
     * 分类表的数据修改
     * @param category
     * @return
     */
    @PutMapping
    public R updateCategory(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改成功");
    }

    /**
     * 分类表的菜品查询
     * @param type
     * @return
     */
    @GetMapping("/list")
    public R listCategory(Integer type){
        //条件构造器,菜品分类/套餐分类
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type!=null,Category::getType,type)
                .orderByAsc(Category::getSort);
        //select * from category where type = ? order by sort asc
        List<Category> list = categoryService.list(queryWrapper);
        return R.success(list);
    }

}
